package com.apache.database.constant;

import com.apache.cache.util.Validator;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.Locale;

public class SpringContextLoader implements ApplicationContextAware {

	private static ApplicationContext context;

	public void setApplicationContext(ApplicationContext arg0) throws BeansException {
		SpringContextLoader.context = arg0;
	}

	public static ApplicationContext getContext() {
		return context;
	}

	public static Object getBean(String name) throws BeansException {
		return context.getBean(name);
	}

	/**
	 * description:  获取国际化消息
	 * @param code   代码
	 * @param params 参数
	 * @param i18nLocale 语言类型(如:zh_CN)
	 * @param defaultMessage 默认值
	 * @return   
	 * @author Hou Dayu  
	 * @update 2015-8-18
	 */
	public static String getMessage(String i18nLocale, String code, String[] params, String defaultMessage) {
		Locale locale = new Locale(i18nLocale);
		String msg = context.getMessage(code, params, locale);
		return Validator.getDefaultStr(msg, defaultMessage);
	}
}
